/*
 *  vscrtext.cpp
 *  framework
 *
 *  Created by gerhard gubler on 22.11.09.
 *  Copyright 2009 md80.ch . All rights reserved.
 *
 */

#include "vscrtext.h"


vscrtext::vscrtext()
{
}

void vscrtext::init()
{
	hundred_up1 = (char*) malloc(char(3));
	hundred_down = (char*) malloc(char(3));
	
	hundred = (char*) malloc(char(3));
	ten = (char*) malloc(char(3));
	one = (char*) malloc(char(3));
	one_up = (char*) malloc(char(3));
	one_down = (char*) malloc(char(3));
	ten_up = (char*) malloc(char(3));
	ten_down = (char*) malloc(char(3));	
	
	memset(hundred_up1,0, 3);
	memset(hundred_down,0, 3);	
	
	memset(hundred, 0, 3);
	memset(ten, 0, 3);
	memset(one,0, 3);
	memset(one_up,0, 3);
	memset(one_down,0, 3);	
	memset(ten_up,0, 3);
	memset(ten_down,0, 3);	
	
}

void vscrtext::display(GLfloat posx, GLfloat posy, float value)
{
	int value2 = (int)value;
	if (value2 < 0)
		value2 = 0;
	
	if (value2 >= 100)
	{
		int bla1 = value2 / 100;
		int bla = bla1 %10;
		sprintf(hundred, "%i", bla);
	}
	else {
		int bla = 0;
		sprintf(hundred, "%i", bla);
	}
	
	if (value2 >= 10)
	{
		int bla1 = value2 / 10;
		int bla = bla1 %10;
		sprintf(ten, "%i", bla);
		value = value - value2;
	}
	else {
		int bla = 0;
		sprintf(ten, "%i", bla);
	}
	
	if (value >= 1)
		value = value - value2;
	
	int bla = value2 % 10;
	sprintf(one, "%i", bla);
	int bla_up = (bla+1)%10;
	sprintf(one_up,"%i", bla_up);
	int bla_down = (bla-1 + 10)%10;
	sprintf(one_down,"%i", bla_down);
	glColor3f(0.0, 1.0, 0.0);
	glLoadIdentity();
	glPushMatrix();
	glRasterPos3f(posx, posy, 0.0);
	
	const char *txt = hundred;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	txt = ten;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}

	glRasterPos3f(posx+25.0, posy - (value *25), 0.0);
	txt = one;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	glRasterPos3f(posx+25.0, posy+25 - (value *25), 0.0);
	txt = one_up;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	glRasterPos3f(posx+25.0, posy-25 - (value *25), 0.0);
	txt = one_down;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	glPopMatrix();
}

void vscrtext::display2(GLfloat posx, GLfloat posy, float value)
{
	int value2 = (int)value;
	if (value2 < 0)
		value2 = 0;
	
	if (value2 >= 100)
	{
		int bla1 = value2 / 100;
		hun_val = bla1 %10;
		sprintf(hundred, "%i", hun_val);
		
		int hundred_bla_up = (hun_val +1) %10;
		sprintf(hundred_up1, "%i", hundred_bla_up);
		int hundred_bla_down = (hun_val -1 +10) %10;
		sprintf(hundred_down, "%i", hundred_bla_down);
	}
	else 
	{
		hun_val = 0;
		sprintf(hundred, " ");
		int hundred_bla_up = (hun_val +1) %10;
		sprintf(hundred_up1, "%i", hundred_bla_up);
		int hundred_bla_down = (hun_val -1 +10) %10;
		sprintf(hundred_down, "%i", hundred_bla_down);
	}
	
	if (value2 >= 8)
	{
		int bla1 = value2 / 10;
		ten_val = bla1 %10;
		sprintf(ten, "%i", ten_val);
		int ten_bla_up = (ten_val +1) %10;
		sprintf(ten_up, "%i", ten_bla_up);
		int ten_bla_down = (ten_val -1 +10) %10;
		sprintf(ten_down, "%i", ten_bla_down);
		value = value - value2;
	}
	else 
	{
		ten_val = 0;
		sprintf(ten, " ");
		int ten_bla_up = (ten_val +1) %10;
		sprintf(ten_up, "%i", ten_bla_up);
		int ten_bla_down = (ten_val -1+10) %10;
		sprintf(ten_down, "%i", ten_bla_down);
	}
	
	if (value >= 1)
		value = value - value2;
	
	one_val = value2 % 10;
	sprintf(one, "%i", one_val);
	int bla_up = (one_val +1)%10;
	sprintf(one_up,"%i", bla_up);
	int bla_down = (one_val -1 + 10)%10;
	sprintf(one_down,"%i", bla_down);
	
	
	glColor3f(0.0, 1.0, 0.0);
	glLoadIdentity();
	glPushMatrix();
	glRasterPos3f(posx, posy, 0.0);

	const char *txt = hundred_up1;
	
	if (ten_val == 9 and one_val == 9)
	{
		glRasterPos3f(posx, posy+25 - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx, posy+25, 0.0);
	}
	
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	if (ten_val == 9 and one_val == 9)
	{
		glRasterPos3f(posx, posy - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx, posy, 0.0);
	}
	txt = hundred;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	if (ten_val == 9 and one_val == 9)
	{
		glRasterPos3f(posx, posy-25 - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx, posy-25, 0.0);
	}
	txt = hundred_down;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	if (one_val == 9)
	{
		glRasterPos3f(posx + 10.0, posy+25 - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx + 10.0, posy+25, 0.0);
	}
	txt = ten_up;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	if (one_val == 9)
	{
		glRasterPos3f(posx + 10.0, posy - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx + 10.0, posy, 0.0);
	}	

	txt = ten;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	if (one_val == 9)
	{
		glRasterPos3f(posx + 10.0, posy-25 - (value *25), 0.0);
	}
	else 
	{
		glRasterPos3f(posx + 10.0, posy-25, 0.0);
	}
	txt = ten_down;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	
	glRasterPos3f(posx+25.0, posy - (value *25), 0.0);
	txt = one;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	glRasterPos3f(posx+25.0, posy+25 - (value *25), 0.0);
	txt = one_up;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	
	glRasterPos3f(posx+25.0, posy-25 - (value *25), 0.0);
	txt = one_down;
	for (const char *loopa = txt; *loopa != '\0'; loopa++)
	{
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24 , *loopa );
	}
	glPopMatrix();
}
